package org.example.course.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.example.commonapi.domain.CourseClass;
import org.example.commonapi.domain.R;
import org.example.commonapi.domain.StudentClass;
import org.example.commonapi.dto.CourseStudentDto;
import org.example.commonapi.dto.StudentDto;
import org.example.course.service.CourseClassService;
import org.example.course.service.StudentClassService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/course")
@Slf4j
public class CourseClassController {

    @Resource
    private CourseClassService courseClassService;

    @Resource
    private StudentClassService studentClassService;

    /**
     * 查询该课程所有班级，可以条件查询
     * @param className
     * @return
     */
    @GetMapping("/list/class/{id}")
    public R courseClassList(@PathVariable("id") Long courseId,String className) {
        List<CourseClass> list=courseClassService.courseClassList(courseId,className);
        log.info("查询该课程所有班级成功，查询到的班级为：{}",list);
        return R.success(list);
    }

    /**
     * 查询改课程所有学生
     * @param courseId
     * @return
     */
    @GetMapping("/list/class/student/{id}")
    public R courseClassStudentList(@PathVariable("id") Long courseId) {
        List<CourseStudentDto> list = courseClassService.courseClassStudentList(courseId);
        return R.success(list);
    }


    /**
     * 老师添加课程班级
     * @param courseId
     * @param className
     * @return
     */
    @PostMapping("/insert/class/{id}")
    public R insertClass(@PathVariable("id") Long courseId, @RequestParam String className) {
//        判断班级是否合法
        if(StringUtils.isEmpty(className)){
            log.error("添加课程班级失败");
            return R.error("班级名不能为空");
        }
//        进行课程添加
        courseClassService.insertClass(courseId,className);
        log.info("添加课程班级成功，添加的班级为：{}",className);
        return R.success();
    }

    /**
     * 修改班级名称（老师）
     * @param courseClass
     * @return
     */
    @PutMapping("/update/class")
    public R updateClass(@RequestBody CourseClass courseClass) {
//        判断修改后的是否合规
        if(StringUtils.isEmpty(courseClass.getClassName()))return R.error("班级名不能为空");
//        修改课程班级
        courseClassService.updateById(courseClass);
        log.info("修改课程班级成功，修改后的班级为：{}",courseClass.getClassName());
        return R.success();
    }

    /**
     * 删除课程班级（老师）
     * @param classId
     * @return
     */
    @DeleteMapping("/delete/class/{id}")
    public R deleteClass(@PathVariable("id") Long classId) {
        courseClassService.removeById(classId);
        LambdaQueryWrapper<StudentClass> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StudentClass::getClassId,classId);
        studentClassService.remove(queryWrapper);
        log.info("删除课程班级成功，删除的班级id为：{}",classId);
        return R.success();
    }

}
